package okl.tdf.app.models
{
	import flashx.textLayout.debug.assert;
	
	import okl.tdf.app.controller.messages.FeedResultMessage;
	import okl.tdf.app.model.AppModel;
	import okl.tdf.app.model.InteractionDisplayModel;
	import okl.tdf.app.model.vo.SampleRequest;
	import okl.tdf.model.InteractionsPerCountry;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNull;

	public class ModelTests
	{		
		[Before]
		public function setUp():void
		{
		}
		
		[After]
		public function tearDown():void
		{
		}
		
		private static const MS_PER_MIN:Number = 60*1000;
		private static function generateNextDate(curDate:Date, maxMin:Number=2):Date{
			return new Date(curDate + Math.random() * (MS_PER_MIN*maxMin));
			
		}
		
		private static function createDDOfromVO(vo:SampleRequest, count:Number=1):InteractionsPerCountry{
			var result:InteractionsPerCountry = new InteractionsPerCountry("UK");
			result.lastResultTime=result.firstResultTime=vo.timestamp;
			result.totalCount=count;
			return result;
		}
			
			
		
		[Test]
		public function interactionDisplayModel_processSampleRequest_newMessage_Test():void{
			var model:InteractionDisplayModel = new InteractionDisplayModel();
			model.appModel=new AppModel();
		
			var vo1:SampleRequest = new SampleRequest();
			vo1.country="UK";
			vo1.timestamp = new Date();
	
			var vo2:SampleRequest = new SampleRequest();
			vo2.country="UA";
			vo2.timestamp = generateNextDate(vo1.timestamp);

			var vo3:SampleRequest = new SampleRequest();
			vo3.country="USA";
			vo3.timestamp = generateNextDate(vo3.timestamp);

			var vo4:SampleRequest = new SampleRequest();
			vo4.country="UA";
			vo4.timestamp = generateNextDate(vo1.timestamp);

			
			var message:FeedResultMessage = new FeedResultMessage();
			message.result = vo1;
			model.processSampleRequest(message);		
			var ddo1:InteractionsPerCountry = createDDOfromVO(vo1);			
			assertEquals(1, model.iteractionsData.length);
			assertEquals(ddo1.firstResultTime,  model.firstResult.firstResultTime);
			assertEquals(ddo1.lastResultTime, model.lastResult.lastResultTime);
			
			
			message = new FeedResultMessage();
			message.result = vo2;
			model.processSampleRequest(message);			
			var ddo2:InteractionsPerCountry = createDDOfromVO(vo2);			
			assertEquals(2, model.iteractionsData.length);
			assertEquals(ddo1.firstResultTime,  model.firstResult.firstResultTime);
			assertEquals(ddo2.lastResultTime, model.lastResult.lastResultTime);
			
			message = new FeedResultMessage();
			message.result = vo3;
			model.processSampleRequest(message);			
			var ddo3:InteractionsPerCountry = createDDOfromVO(vo3);		
			assertEquals(3, model.iteractionsData.length);
			assertEquals(ddo1.firstResultTime,  model.firstResult.firstResultTime);
			assertEquals(ddo3.lastResultTime, model.lastResult.lastResultTime);
			
			message = new FeedResultMessage();
			message.result = vo4;
			model.processSampleRequest(message);			
			var ddo4:InteractionsPerCountry = createDDOfromVO(vo4);			
			assertEquals(3, model.iteractionsData.length); //not changed, country already exist
			assertEquals(ddo1.firstResultTime,  model.firstResult.firstResultTime);
			assertEquals(ddo4.lastResultTime, model.lastResult.lastResultTime); //but time updated
		}
		
		[Test]
		public function interactionDisplayModel_processSampleRequest_unsubscribe_Test():void{
			var model:InteractionDisplayModel = new InteractionDisplayModel();
			model.firstResult = new InteractionsPerCountry("UK");
			model.lastResult = new InteractionsPerCountry("UA");
			
			var message:FeedResultMessage = new FeedResultMessage();
			message.subscribed=false;
			message.result = new SampleRequest();
			model.processSampleRequest(message);
			
			assertNull(model.iteractionsData);
			assertNull(model.firstResult);
			assertNull(model.lastResult);
		}
		
		
	}
}